package ProjectCodeEngine;

import model.EvaluationResult;
import org.kie.soup.commons.util.Lists;

import java.util.ArrayList;
import java.util.List;

import static ProjectCodeEngine.CodeType.COMBINE_WELDING;
import static ProjectCodeEngine.CodeType.MANUAL_WELDING;
import static ProjectCodeEngine.CodeType.MECHANICAL_WELDING;

/**
 * [main] 焊接技能评定代号解析器启动类
 *
 * @author zzy
 * @version V1.0
 * @since 2022/8/4
 */
public class EvaluationStarter {

    /**
     * main function for parse project code
     *
     * @param projectCode 项目代号
     * @param splitRule   项目代号切分规则器
     * @return
     */
    public static List<EvaluationResult> doParse(String projectCode, SplitRule splitRule) {
        // 判断类型
        String type = new DefaultTypeJudgeRule().getType(projectCode);

        // 组合焊
        if (COMBINE_WELDING.equals(type)) {
            SplitRule rule = new SplitCombineRule();
            return of(rule.split(projectCode), splitRule);
        }

        // 非组合焊
        EvaluationResult of = of(type, splitRule.split(projectCode));
        return buildList(of);
    }

    /**
     * 组合焊的拆分
     */
    private static List<EvaluationResult> of(String[] args, SplitRule splitRule) {
        ArrayList<EvaluationResult> ofs = new ArrayList<EvaluationResult>();
        ofs.add(of(new DefaultTypeJudgeRule().getType(args[0]), splitRule.split(args[0])));
        ofs.add(of(new DefaultTypeJudgeRule().getType(args[1]), splitRule.split(args[1])));
        return ofs;
    }

    /**
     * 非组合焊封装
     */
    private static List<EvaluationResult> buildList(EvaluationResult evaluationResult) {
        ArrayList<EvaluationResult> ofs = new ArrayList<EvaluationResult>();
        ofs.add(evaluationResult);
        return ofs;
    }

    /**
     * 焊接基础类型解析器启动器
     */
    private static EvaluationResult of(String type, String[] args) {
        // 手工焊
        if (MANUAL_WELDING.equals(type)) {
            ParseProjectCode parseProjectCode = new ParseManualWelding();
            return parseProjectCode.parse(args, type);
        }
        // 机械化焊
        else if (MECHANICAL_WELDING.equals(type)) {
            ParseProjectCode parseProjectCode = new ParseMechanicalWelding();
            return parseProjectCode.parse(args, type);
        }
        // 自动焊
        else {
            ParseProjectCode parseProjectCode = new ParseAutomaticWelding();
            return parseProjectCode.parse(args, type);
        }
    }

}
